#!/usr/bin/env python3.9
# coding: utf-8
"""
提前完成以下：
1.修改a2f配置文件
路径：D:/omniverse/pkg/audio2face-2021.3.1/apps/audio2face.kit  位置：63-66  修改内容：固定窗口位置(0,0), 大小为1300x600
2.修改a2f源码
路径：D:/omniverse/pkg/audio2face-2021.3.1/exts/omni.audio2face.tool/omni/audio2face/tool/scripts/exporter.py  位置：28-183, 1331-1357  修改内容：json数据直接发送到服务端
"""
from uiautomation import WindowControl
import pyautogui
import os
import time
import clipboard

Kit_path = "D:/omniverse/pkg/audio2face-2021.3.1/kit/kit.exe"
Config_path = "D:/omniverse/pkg/audio2face-2021.3.1/apps/audio2face.kit"
Script_dir = "D:/project/audio2face/scripts"
Model_path = "D:/project/audio2face/models/test.usd"
Voice_dir = "D:/project/audio2face/wav"
Voice_idx = 1
Fps = 60

def __get_window(subname):
    """
    获取句柄
    """
    window = WindowControl(searchDepth=1, SubName=subname)
    # window.SwitchToThisWindow()
    return window

def __clean_editor():
    """
    清理脚本编辑器
    """
    pyautogui.moveTo(81,91)  # Tab
    pyautogui.click()
    pyautogui.moveTo(123,203)  # Close 
    pyautogui.click()
  
def __init_edit_env():
    """
    初始化脚本编辑环境
    """
    window = __get_window("Audio2Face")  # 0s
    window.SwitchToThisWindow(0)  # 0.2s
    pyautogui.moveTo(253,10)  # Windows
    pyautogui.click()
    pyautogui.moveTo(304,159)  # Script Editor
    pyautogui.click()
    pyautogui.moveTo(400,400)  # Edit
    pyautogui.click()
    window.SendKeys("omni.kit.app.get_app().get_python_scripting().add_search_script_folder('{}')".format(Script_dir), 0, 0)  # pyautogui受输入法影响
    window.SendKeys('{Enter}', 0, 0)
    pyautogui.moveTo(118,639)  # Run
    pyautogui.click()
    __clean_editor()

def start_a2f(kit_path, config_path):
    """
    启动audio2face软件
    """
    os.system("start cmd")
    window = __get_window("cmd.exe")
    window.SendKeys(Kit_path + " " + Config_path, 0, 0)
    window.SendKeys('{Enter}', 0, 0)
    while True:
        time.sleep(4)
        window.SendKeys('{Ctrl}A', 0, 0)
        window.SendKeys('{Ctrl}C',0, 0)
        result = clipboard.paste()
        if "[omni.audio2face.core] [INFO] Resampling A2F track" in result:
            break
    __init_edit_env()
    print("Sucesses start Audio2Face")

def end_a2f():
    """
    关闭已经启动的audio2face程序
    """
    
    window = __get_window("cmd.exe")
    window.SendKeys('{alt}{f4}', 0, 0)
    print("Sucesses end Audio2Face")

def change_model(path):
    """
    更换模型
    """
    window = __get_window("Audio2Face")  # 0s
    window.SwitchToThisWindow(0)  # 0.2s  切换到程序 防止弹窗覆盖
    pyautogui.moveTo(400,400)  # Edit
    pyautogui.click()
    window.SendKeys("omni.kit.app.get_app().get_python_scripting().execute_file('change_model.py', ['{}'])".format(path), 0, 0)
    pyautogui.moveTo(118,639)  # Run
    pyautogui.click()
    time.sleep(2)  # 更换时间
    window = __get_window(path.split("/")[-1])  # 查找是否更换成功
    if window:
        print("Sucesses!", window.Name)
    __clean_editor()

def change_voice_dir(path):
    """
    更换音频文件夹
    """
    window = __get_window("Audio2Face")  # 0s
    window.SwitchToThisWindow(0)  # 0.2s  切换到程序 防止弹窗覆盖
    pyautogui.moveTo(400,400)  # Edit
    pyautogui.click()
    window.SendKeys("omni.kit.app.get_app().get_python_scripting().execute_file('change_voice_dir.py', ['{}'])".format(path), 0, 0)
    pyautogui.moveTo(118,639)  # Run
    pyautogui.click()
    __clean_editor()

def voice_to_bs_json(index, fps):
    """
    当前音频文件转换为blendshape动画帧的json数据
    audio2face源码修改后，自动发送json数据到服务端
    """
    window = __get_window("Audio2Face")  # 0s
    window.SwitchToThisWindow(0)  # 0.2s  切换到程序 防止弹窗覆盖
    pyautogui.moveTo(400,400)  # Edit
    pyautogui.click()
    window.SendKeys("omni.kit.app.get_app().get_python_scripting().execute_file('voice_to_bs_json.py', ['{}', '{}'])".format(str(index), str(fps)), 0, 0)
    pyautogui.moveTo(118,639)  # Run
    pyautogui.click()
    __clean_editor()

if __name__ == '__main__':
    start_a2f(Kit_path, Config_path)
    change_model(Model_path)
    change_voice_dir(Voice_dir)
    voice_to_bs_json(Voice_idx, Fps)
    # end_a2f()